package net.begincode.admin.controller;

import net.begincode.admin.bean.KeyWordEditParam;
import net.begincode.admin.bean.KeyWordIdParam;
import net.begincode.admin.bean.KeyWordParam;
import net.begincode.biz.KeyWordsBiz;
import net.begincode.blog.bean.param.KeyWordsQueryParam;
import net.begincode.blog.entity.KeyWords;
import net.begincode.blog.entity.User;
import net.begincode.blog.enums.KeyWordFlagEnum;
import net.begincode.common.ApiResponse;
import net.begincode.design.BizTemplate;
import net.begincode.page.PageRequest;
import net.begincode.page.PageResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * 关键字管理
 */
@Controller
@RequestMapping("/admin/keyWords/")
public class KeyWordsController {
    @Resource
    private KeyWordsBiz keyWordsBiz;

    /**
     * 新增关键字
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public ApiResponse<Boolean> add(KeyWordParam keyWordAddParam) {
        keyWordAddParam.checkParam();
        KeyWords keyWords = new KeyWords();
        keyWords.setKeyWord(keyWordAddParam.getKeyWord());
        keyWords.setFlag(KeyWordFlagEnum.EFFECT.getFlagCode());
        int addCount = keyWordsBiz.addKeyWords(keyWords);
        if (addCount > 0) {
            return ApiResponse.success(true);
        }
        return ApiResponse.fail(false);
    }

    /**
     * 更新关键字
     */
    @RequestMapping("update")
    public ModelAndView update(KeyWordEditParam keyWordEditParam) {
        keyWordEditParam.checkParam();
        KeyWords keyWords = new KeyWords();
        keyWords.setKeyWord(keyWordEditParam.getKeyWord());
        keyWords.setId(keyWordEditParam.getKeyWordId());
        keyWordsBiz.updateKeyWordsById(keyWords);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("keyWordList");
        return modelAndView;
    }

    /**
     * 关键字生效
     */

    @ResponseBody
    @RequestMapping(value = "checkEffect", method = RequestMethod.POST)
    public ApiResponse<Boolean> effectKeyWord(KeyWordIdParam keyWordIdParam) {
        KeyWords keyWords = new KeyWords();
        keyWords.setId(keyWordIdParam.getKeyWordId());
        keyWords.setFlag(KeyWordFlagEnum.EFFECT.getFlagCode());
        int updateCount = keyWordsBiz.updateKeyWordsById(keyWords);
        if (updateCount > 0) {
            return ApiResponse.success(true);
        }
        return ApiResponse.fail(false);
    }

    /**
     * 关键字失效
     */
    @ResponseBody
    @RequestMapping(value = "checkUnEffect", method = RequestMethod.POST)
    public ApiResponse<Boolean> unEffectKeyWord(KeyWordIdParam keyWordIdParam) {
        KeyWords keyWords = new KeyWords();
        keyWords.setId(keyWordIdParam.getKeyWordId());
        keyWords.setFlag(KeyWordFlagEnum.UNEFFECT.getFlagCode());
        int updateCount = keyWordsBiz.updateKeyWordsById(keyWords);
        if (updateCount > 0) {
            return ApiResponse.success(true);
        }
        return ApiResponse.fail(false);
    }


    /**
     * 查询有效关键字
     */
    @RequestMapping("queryKeyWords")
    @ResponseBody
    public ApiResponse<List<KeyWords>> queryKeyWords(KeyWordsQueryParam keyWordsQueryParam) {

        return new BizTemplate<ApiResponse<List<KeyWords>>>("KeyWordsController.queryKeyWords") {
            @Override
            public void checkParam() {
                super.checkParam();
            }

            @Override
            public ApiResponse<List<KeyWords>> process() {
                PageResponse<KeyWords> keyWordsPage = keyWordsBiz.queryKeyWords(keyWordsQueryParam);
                return ApiResponse.success(keyWordsPage.getData(), keyWordsPage.getTotalSize());
            }
        }.excute();
    }

    /**
     * 查询有效关键字
     */
    @RequestMapping("queryEffectKeyWords")
    @ResponseBody
    public ApiResponse<List<KeyWords>> queryEffectKeyWords() {
        KeyWordsQueryParam keyWordsQueryParam = new KeyWordsQueryParam();
        keyWordsQueryParam.setKeyWordFlag(KeyWordFlagEnum.EFFECT.getFlagCode());
        keyWordsQueryParam.setPageSize(1000);
        return new BizTemplate<ApiResponse<List<KeyWords>>>("KeyWordsController.queryEffectKeyWords") {
            @Override
            public void checkParam() {
                super.checkParam();
            }

            @Override
            public ApiResponse<List<KeyWords>> process() {
                PageResponse<KeyWords> keyWordsPage = keyWordsBiz.queryKeyWords(keyWordsQueryParam);
                return ApiResponse.success(keyWordsPage.getData(), keyWordsPage.getTotalSize());
            }
        }.excute();
    }
}
